﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;
using BLL;
using System.Data;
using System.Web.Services;
using System.IO;

public partial class _Default : System.Web.UI.Page
{

    ProductBLL dataProduct = new ProductBLL();
    SizeBLL dataSize = new SizeBLL();
    BrandCateBLL dataBrandCate = new BrandCateBLL();
    protected void Page_Load(object sender, EventArgs e)
    {
        LoadData();
    }


    [WebMethod]
    public static int Login(string email, string pass)
    {
        UserBLL dataUser = new UserBLL();
        if (!dataUser.isCheckLogin(email, Myfunction.Encrypt(pass)))
        {
            return 0;
        }
        else
        {
            return 1;
            //User user = dataUser.GetByUserName(email);
            //Page page = HttpContext.Current.Handler as Page;

            //Control control = null;

            //if (page != null)
            //{
            //    var c = page.FindControl("hplName");
                
            //}

            //Session["Member"] = user;
            //hplName.Text = user.Name;
            //Hello.Visible = true;
            //Register.Visible = false;
            //hplName.Text = ((DAL.User)Session["Member"]).Name;
            
        }

    }

    private void LoadData()
    {
        LoadRptProductNew();
        LoadRptProductBuy();
        LoadRptBrandHot();
        LoadRptBrandHot2();
    }

    void LoadRptBrandHot()
    {
        int top = 8;
        int skip = 0;
        var listBrand = dataBrandCate.GetByTop(top, skip);
        DataTable tb = Myfunction.ConvertToDataTable(listBrand);
        rptBrandHot1.DataSource = tb;
        rptBrandHot1.DataBind();
    }

    void LoadRptBrandHot2()
    {
        int top = 16;
        int skip = 8;
        var listBrand = dataBrandCate.GetByTop(top, skip);
        DataTable tb = Myfunction.ConvertToDataTable(listBrand);
        rptBrandHot2.DataSource = tb;
        rptBrandHot2.DataBind();
    }

    void LoadRptProductNew()
    { 
        int top = 8;
        var listProduct = dataProduct.GetByTopNew(top, true, true);
        if (listProduct.Count == 0)
            divProductNew.Visible = false;
        else
        {
            DataTable tb = Myfunction.ConvertToDataTable(listProduct);
            rptNewProduct.DataSource = tb;
            rptNewProduct.DataBind();

            //if (listProduct.Count <= 20)
                //divViewMoreNew.Visible = false;
        }
    }
    void LoadRptProductBuy()
    {
        int top = 8;
        var listProduct = dataProduct.GetByTopBuy(top, true, true);
        if (listProduct.Count == 0)
            divProductBuy.Visible = false;
        else
        {
            DataTable tb = Myfunction.ConvertToDataTable(listProduct);
            rptProductBuy.DataSource = tb;
            rptProductBuy.DataBind();

            //if (listProduct.Count <= 20)
            //    divViewMore.Visible = false;
        }
    }

    protected void rptNewProduct_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        Label lbProductID = (Label)e.Item.FindControl("lbId");
        string productId = lbProductID.Text.ToString();

        var listSize = dataSize.GetByProductId(productId);
        DataTable tbSize = Myfunction.ConvertToDataTable(listSize);
        
        Repeater rptSize = (Repeater)e.Item.FindControl("rptSize");
        rptSize.DataSource = tbSize;
        rptSize.DataBind();
    }
    protected void rptProductBuy_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        Label lbProductID = (Label)e.Item.FindControl("ldIdBuy");
        string productId = lbProductID.Text.ToString();

        var listSize = dataSize.GetByProductId(productId);
        DataTable tbSize = Myfunction.ConvertToDataTable(listSize);

        Repeater rptSize = (Repeater)e.Item.FindControl("rptSizeBuy");
        rptSize.DataSource = tbSize;
        rptSize.DataBind();
    }
}